package cc.rengu.igas.cops.core.service.trans.asyncall;
/*
 *Create by zhangbin on 2020-9-11
 */

import cc.rengu.igas.acct.facade.request.VirtAcctSingleAdjustRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctSingleAdjustResponse;
import cc.rengu.igas.acct.facade.result.Result;
import cc.rengu.igas.cops.common.constant.CopsTreeNodeConstant;
import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.GlobalTransUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CopsDealAsyncVirtAcctSingleAdjust extends DubboAsynBaseService {
    @BIZ_PROCESS
    public void copsDealAsyncVirtAcctSingleAdjust() throws Exception {
        rglog.debug(">>>>>>>>>>>登记簿单笔记账处理开始<<<<<<<<<<<<");
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        VirtAcctSingleAdjustRequest request = (VirtAcctSingleAdjustRequest) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_REQUEST, VirtAcctSingleAdjustRequest.class);
        if (null == request) {
            rglog.debug("请求报文异常");
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        String mchntNo = request.getUserId();
        String sysSeqNum = request.getHeader().getTraceNo().substring(0, 32);
        rglog.debug("单笔记账instId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>", instId, mchntNo, sysSeqNum);
        Result<VirtAcctSingleAdjustResponse> virtAcctSingleAdjustResponseResult = (Result) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, Result.class);
        if (null == virtAcctSingleAdjustResponseResult) {
            /*记虚拟账dubbo调用失败*/
            rglog.debug("登记簿记账失败mchnt:{} sysSeqNum{}", mchntNo, sysSeqNum);
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        /*jsonobject转成javaobject节点处理*/
        VirtAcctSingleAdjustResponse response = (VirtAcctSingleAdjustResponse) JSONObject.parseObject(JSONObject.toJSONString(virtAcctSingleAdjustResponseResult.getResult()), VirtAcctSingleAdjustResponse.class);
        /*JSON.toJavaObject()*/
        if (null == response) {
            /*记虚拟账dubbo调用失败*/
            rglog.debug("登记簿记账失败mchnt:{} sysSeqNum{}", mchntNo, sysSeqNum);
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }

        MchntOrderInfo mchntOrderInfo = (MchntOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.MCHNT_ORDER_INFO, MchntOrderInfo.class);
        OrderService orderService = new OrderServiceImpl();
//        MchntOrderInfoMapper mchntOrderInfoMapper=new MchntOrderInfoMapperImpl();
//        /*成功处理,1:更新商户订单表中相关金额，更新虚拟账户支付订单表中相关金额，调用微信推送功能*/
//           MchntOrderInfo mchntOrderInfo=mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(instId,sysSeqNum,mchntNo);
//            if(null==mchntOrderInfo){
//                rglog.debug("获取商户订单表异常,流水号：<{}>,商户号：<{}>",sysSeqNum,mchntNo);
//                throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(),OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
//            }
        /*获取虚拟账户对应支付订单表*/
        PayOrderInfo virtAcctPayOrderInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.VIRT_ACCT_PAY_OORDER_INFO, PayOrderInfo.class);
        if (null == virtAcctPayOrderInfo) {
            rglog.error("获取单笔记账支付订单表异常");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }
        /*调用虚拟账户记账成功处理*/
        virtAcctPayOrderInfo.setRespCode(response.getRespCode());
        virtAcctPayOrderInfo.setRespDesc(response.getRespDesc());
        if (RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(response.getRespCode())) {
            rglog.debug("<mchntNo:<{}>,sysSeqNum<{}>调用单笔记账成功，更新商户订单和记账支付订单>", mchntNo, sysSeqNum);
            mchntOrderInfo.setVirtualAcctBalance(response.getAfterAvailableBalance());
            /*更新商户订单表*/
            List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
            mchntOrderInfoList.add(mchntOrderInfo);
            /*设置虚拟账户支付订单的状态*/
            virtAcctPayOrderInfo.setTransStatus("1");
            virtAcctPayOrderInfo.setBizStatus("00");
            /*更新虚拟账户支付订单表*/
            orderService.updateOrderInfo(null, mchntOrderInfoList, virtAcctPayOrderInfo);
            /*调用微信推送*/
            /*记账成功后，调用微信消息推送*/
            /*获取用户订单 并放入内部树 以供 share层 消息推送 获取使用*/
            UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
            UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, sysSeqNum);
            if (null == userOrderInfo) {
                rglog.debug("查询用户订单表异常");
                throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
            }
            GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, userOrderInfo);
            int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
            if (0 != iReturnCode) {
                rglog.error("提交交易结果异步通知给商户失败!");
            }
        } else if (OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode().equals(response.getRespCode())) {
            rglog.debug("<acctAdjust登记簿记账超时>");
            /*失败更新状态*/
            /*设置虚拟账户支付订单的状态*/
            virtAcctPayOrderInfo.setTransStatus("3");
            virtAcctPayOrderInfo.setBizStatus("00");
            /*更新虚拟账户支付订单表*/
            orderService.updateOrderInfo(null, null, virtAcctPayOrderInfo);
        } else {
            rglog.debug("<acctAdjust登记簿记账失败>");
            /*设置虚拟账户支付订单的状态*/
            virtAcctPayOrderInfo.setTransStatus("2");
            virtAcctPayOrderInfo.setBizStatus("02");
            /*更新虚拟账户支付订单表*/
            orderService.updateOrderInfo(null, null, virtAcctPayOrderInfo);
        }
    }
}
